package com.lvmama.rhino.base;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/*
 * 序列化工具
 */
public class SerializeUtil {
	
	/*
	 * 序列化
	 */
	public static byte[] serialize(Object obj) {
		ByteArrayOutputStream bop = new ByteArrayOutputStream();
		try {
			ObjectOutputStream oop = new ObjectOutputStream(bop);
			oop.writeObject(obj);
			byte[] objByte = bop.toByteArray();
			return objByte;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/*
	 * 反序列化
	 */
	public static Object deSerialize(byte[] byteArray) {
		ByteArrayInputStream bip = new ByteArrayInputStream(byteArray);
		try {
			ObjectInputStream oip = new ObjectInputStream(bip);
			Object obj = oip.readObject();
			return obj;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
